package com.xcdh.trading.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xcdh.commons.utils.RestTemplateUtil;
import com.xcdh.trading.entity.AStock;
import com.xcdh.trading.mapper.AStockMapper;
import com.xcdh.trading.query.StockPayload;
import com.xcdh.trading.response.TradingRes;
import com.xcdh.trading.service.AStockService;
import com.xcdh.trading.util.TuShareApi;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class AStockServiceImpl extends ServiceImpl<AStockMapper, AStock> implements AStockService {

    @Override
    public void downLoadAStock() {
        List<String> fields = Arrays.asList("ts_code", "symbol", "name", "area", "industry", "cnspell", "market",
                "list_date", "act_name", "act_ent_type", "enname", "fullname", "exchange", "curr_type",
                "list_status", "is_hs", "delist_date");
        StockPayload payload = new StockPayload() ;
        payload.setFields(fields);
        payload.setApi_name("stock_basic");
        TradingRes tradingRes = RestTemplateUtil.post("http://api.tushare.pro", payload, TradingRes.class);
        List<AStock> list = tradingRes.parse(AStock.class);
        for (AStock aStock : list) {
            if(aStock.getName() != null){
                saveOrUpdate(aStock) ;
            }
        }
    }

    @Override
    public void syncAstock() {
        TradingRes tradingRes = TuShareApi.post("stock_basic");
        List<AStock> resList = tradingRes.parse(AStock.class);

        for (AStock aStock : resList) {
            if(aStock.getName() != null){
                saveOrUpdate(aStock) ;
            }
        }
    }


}
